package org.example.env_boot.controller;

import org.example.env_boot.dto.ApiResponse;
import org.example.env_boot.entity.AqiFeedback;
import org.example.env_boot.service.SupervisorService;
import org.example.env_boot.util.JwtUtil;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 公众监督员控制器
 */
@RestController
@RequestMapping("/supervisor")
@CrossOrigin(origins = "*")
public class SupervisorController {
    
    private final SupervisorService supervisorService;
    private final JwtUtil jwtUtil;
    
    public SupervisorController(SupervisorService supervisorService, JwtUtil jwtUtil) {
        this.supervisorService = supervisorService;
        this.jwtUtil = jwtUtil;
    }
    
    /**
     * 提交反馈信息
     */
    @PostMapping("/feedback")
    public ApiResponse<AqiFeedback> submitFeedback(@RequestBody SubmitFeedbackRequest request,
                                                   Authentication authentication) {
        Integer supervisorId = Integer.parseInt(authentication.getDetails().toString()); // 从JWT中获取监督员ID
        
        return supervisorService.submitFeedback(
            supervisorId,
            request.getProvinceId(),
            request.getCityId(),
            request.getAqiId(),
            request.getSo2Density(),
            request.getCoDensity(),
            request.getSpmDensity(),
            request.getRemarks()
        );
    }
    
    /**
     * 获取历史反馈信息
     */
    @GetMapping("/feedback/history")
    public ApiResponse<List<AqiFeedback>> getFeedbackHistory(Authentication authentication) {
        Integer supervisorId = Integer.parseInt(authentication.getDetails().toString());
        return supervisorService.getFeedbackHistory(supervisorId);
    }
    
    /**
     * 获取反馈统计信息
     */
    @GetMapping("/feedback/statistics")
    public ApiResponse<SupervisorService.FeedbackStatistics> getFeedbackStatistics(Authentication authentication) {
        Integer supervisorId = Integer.parseInt(authentication.getDetails().toString());
        return supervisorService.getFeedbackStatistics(supervisorId);
    }
    
    /**
     * 获取反馈详情
     */
    @GetMapping("/feedback/{feedbackId}")
    public ApiResponse<AqiFeedback> getFeedbackDetail(@PathVariable Integer feedbackId,
                                                      Authentication authentication) {
        Integer supervisorId = Integer.parseInt(authentication.getDetails().toString());
        return supervisorService.getFeedbackDetail(supervisorId, feedbackId);
    }
    
    /**
     * 获取当前监督员信息
     */
    @GetMapping("/profile")
    public ApiResponse<?> getProfile(@RequestHeader("Authorization") String authorization) {
        if (authorization != null && authorization.startsWith("Bearer ")) {
            String token = authorization.substring(7);
            return supervisorService.getFeedbackStatistics(Integer.parseInt(jwtUtil.getUserIdFromToken(token)));
        }
        return ApiResponse.unauthorized("未提供有效的认证信息");
    }
    
    // 请求类定义
    public static class SubmitFeedbackRequest {
        @NotNull(message = "省份ID不能为空")
        private Integer provinceId;
        
        @NotNull(message = "城市ID不能为空")
        private Integer cityId;
        
        @NotNull(message = "AQI级别ID不能为空")
        private Integer aqiId;
        
        @NotNull(message = "SO2浓度不能为空")
        private Integer so2Density;
        
        @NotNull(message = "CO浓度不能为空")
        private Integer coDensity;
        
        @NotNull(message = "PM2.5浓度不能为空")
        private Integer spmDensity;
        
        private String remarks;
        
        public Integer getProvinceId() { return provinceId; }
        public void setProvinceId(Integer provinceId) { this.provinceId = provinceId; }
        public Integer getCityId() { return cityId; }
        public void setCityId(Integer cityId) { this.cityId = cityId; }
        public Integer getAqiId() { return aqiId; }
        public void setAqiId(Integer aqiId) { this.aqiId = aqiId; }
        public Integer getSo2Density() { return so2Density; }
        public void setSo2Density(Integer so2Density) { this.so2Density = so2Density; }
        public Integer getCoDensity() { return coDensity; }
        public void setCoDensity(Integer coDensity) { this.coDensity = coDensity; }
        public Integer getSpmDensity() { return spmDensity; }
        public void setSpmDensity(Integer spmDensity) { this.spmDensity = spmDensity; }
        public String getRemarks() { return remarks; }
        public void setRemarks(String remarks) { this.remarks = remarks; }
    }
} 